package com.ruoyi.web.controller.module.estate;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.EstateBuildUnit;
import com.ruoyi.system.service.module.estate.IEstateBuildUnitService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 楼宇单元数据Controller
 *
 * @author ruoyi
 * @date 2024-01-18
 */
@Validated
@Api(value = "楼宇单元数据控制器", tags = {"楼宇单元数据管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/estate/build/unit")
public class EstateBuildUnitController extends BaseController {

    private final IEstateBuildUnitService estateBuildUnitService;

    /**
     * 查询楼宇单元数据列表
     */
    @ApiOperation("查询楼宇单元数据列表")
    @PreAuthorize("@ss.hasPermi('module:buildUnit:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateBuildUnit> list(@Validated EstateBuildUnit estateBuildUnit) {
        QueryWrapper<EstateBuildUnit> qw = new QueryWrapper<>();
		if(estateBuildUnit.getBuildId()!=null){
			qw.eq("build_id", estateBuildUnit.getBuildId());
		}
		qw.orderByAsc("sort");

        TableDataInfo<EstateBuildUnit> td = PageUtils.buildDataInfo(estateBuildUnitService.page(PageUtils.buildPage(),qw));
        List<EstateBuildUnit> list = td.getRows();
        return td;
    }

    /**
     * 导出楼宇单元数据列表
     */
    @ApiOperation("导出楼宇单元数据列表")
    @PreAuthorize("@ss.hasPermi('module:buildUnit:export')")
    @Log(title = "楼宇单元数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateBuildUnit> export(@Validated EstateBuildUnit estateBuildUnit) {
        List<EstateBuildUnit> list = estateBuildUnitService.list();
        ExcelUtil<EstateBuildUnit> util = new ExcelUtil<EstateBuildUnit>(EstateBuildUnit.class);
        return util.exportExcel(list, "楼宇单元数据");
    }

    /**
     * 获取楼宇单元数据详细信息
     */
    @ApiOperation("获取楼宇单元数据详细信息")
    @PreAuthorize("@ss.hasPermi('module:buildUnit:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateBuildUnit> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateBuildUnit estateBuildUnit = estateBuildUnitService.getById(id);
        return AjaxResult.success(estateBuildUnit);
    }

    /**
     * 新增楼宇单元数据
     */
    @ApiOperation("新增楼宇单元数据")
    @PreAuthorize("@ss.hasPermi('module:buildUnit:add')")
    @Log(title = "楼宇单元数据", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateBuildUnit estateBuildUnit) {
        return toAjax(estateBuildUnitService.save(estateBuildUnit) ? 1 : 0);
    }

    /**
     * 修改楼宇单元数据
     */
    @ApiOperation("修改楼宇单元数据")
    @PreAuthorize("@ss.hasPermi('module:buildUnit:edit')")
    @Log(title = "楼宇单元数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateBuildUnit estateBuildUnit) {
        return toAjax(estateBuildUnitService.updateById(estateBuildUnit) ? 1 : 0);
    }

    /**
     * 删除楼宇单元数据
     */
    @ApiOperation("删除楼宇单元数据")
    @PreAuthorize("@ss.hasPermi('module:buildUnit:remove')")
    @Log(title = "楼宇单元数据" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateBuildUnitService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
